from tkinter import *
from tkinter.messagebox import *
import pymysql


def logDB(name):
    # 1 连接数据库
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456", db="pydemodb", charset="utf8")
    # 2 获取游标
    cursor = conn.cursor()
    # 3 执行sql
    sclSql = f"select * from people where name='{name}'"
    cursor.execute(sclSql)
    date = cursor.fetchone()
    # 4 提交事务
    conn.commit()
    # 5 释放资源
    cursor.close()
    conn.close()
    return date


def logUser():
    # 获取文本框输入的账号密码
    useName = u_name.get()
    usePwd = u_pwd.get()
    date = logDB(useName)
    if date:
        if usePwd == date[1]:
            showinfo("成功", message="登录成功")
        else:
            showerror("失败", message="密码错误！！！")
    else:
        showerror("失败", message="账号错误！！！")


def exits():
    wim.destroy()  # 关闭窗口

wim = Tk()  # 创建窗口
wim.title("模拟登录MySQL")
wim.geometry("500x500+600+300")  # 设置窗口大小
# 设置标签
Label(wim, text="账 号：", font=("宋体", 18), fg="#698B69").grid(row=0, column=0)
Label(wim, text="密 码：", font=("宋体", 18), fg="#698B69").grid(row=1, column=0)
# 设置文本框和密码框
u_name = StringVar()
Entry(wim, font=("宋体", 18), textvariable=u_name).grid(row=0, column=1)
u_pwd = StringVar()
# 不显示明文密码： show = "*"
Entry(wim, font=("宋体", 18), textvariable=u_pwd).grid(row=1, column=1)
# 设置按钮
Button(wim, text="登录", font=("宋体", 18), command=logUser).place(x=90, y=75)
Button(wim, text="取消", font=("宋体", 18),command=exits).place(x=180, y=75)

wim.mainloop()  # 显示窗口
